/* $RCSFile: PortalUrlDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/PortalUrlDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Helpers;
using AddonNice.Diagnostics;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// PortalUrlDataType
	/// </summary>
	public class PortalUrlDataType : StringDataType
	{
		private string _portalPathPrefix    =   string.Empty;
        bool PrefixAsPorTalPath             =   true;

		public PortalUrlDataType()
		{
			InnerDataType       =   PropertiesDataType.String;
		}

		/// <summary>
		/// Use this Ctor on portalsetting or when you want turn off automatic discovery
		/// </summary>
		/// <param name="PortalFullPath"></param>
		public PortalUrlDataType(string portalPathPrefix)
		{
			InnerDataType       =   PropertiesDataType.String;
			_portalPathPrefix   =   portalPathPrefix;
		}

		public PortalUrlDataType(string portalPathPrefix,bool prefixAsPorTalPath)
		{
			InnerDataType       =   PropertiesDataType.String;
			_portalPathPrefix   =   portalPathPrefix;
            PrefixAsPorTalPath  =   prefixAsPorTalPath;
		}

		/// <summary>
		/// PortalFullPath (path to current portal: i.e. ~\portals\_currentPortal ) with a ending '/'
		/// </summary>
        protected string PortalPathPrefix
		{
			get
			{
				if (string.IsNullOrEmpty(_portalPathPrefix))
				{
					// Obtain PortalSettings from Current Context
					try
					{
						if (PrefixAsPorTalPath && portalSettings != null)
						{
							_portalPathPrefix       =   portalSettings.PortalFullPath;
							if (!_portalPathPrefix.EndsWith("/"))
								_portalPathPrefix   +=  "/";
						}
					}
					catch (Exception exp)
					{
                        if ( DataTypeTraceSwitch.Sw.TraceError )
                            Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PortalUrlDataType PortalPathPrefix ex : {0}", exp),DataTypeTraceSwitch.Sw.Info);
                        throw;
					}
				}
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PortalUrlDataType PortalPathPrefix GET _portalPathPrefix: {0}", _portalPathPrefix),DataTypeTraceSwitch.Sw.Info);
				return _portalPathPrefix;
			}	
		}

		private string _innerFullPath;
		/// <summary>
		/// Combine PortalPathPrefix and Val to give a full path
		/// </summary>
		public override string FullPath
		{
			get
			{
				if(_innerFullPath == null )
				{
					_innerFullPath  =   AddonNice.Settings.Path.WebPathCombine(PortalPathPrefix, Val);
					_innerFullPath  =   _innerFullPath.TrimEnd('/'); //Removes trailings
				}
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PortalUrlDataType FullPath GET _innerFullPath: {0}", _innerFullPath),DataTypeTraceSwitch.Sw.Info);
				return _innerFullPath;
			}	
		}

		public override string Val
		{
			get
			{
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PortalUrlDataType Val GET _portalPathPrefix: {0}, innerValue: {1}", _portalPathPrefix,innerValue),DataTypeTraceSwitch.Sw.Info);
				if ( !PortalPathPrefix.Equals(string.Empty) && innerValue.StartsWith(PortalPathPrefix))
					return innerValue.Substring(_portalPathPrefix.Length);
				return innerValue;
			}
			set
			{
                string nval                 =   value.TrimStart(new char[]{'~'}).ToLower();
				//Remove portal path if present
                if ( !PortalPathPrefix.Equals(string.Empty) && nval.StartsWith(PortalPathPrefix,StringComparison.InvariantCultureIgnoreCase))
                    innerValue              =   nval.Substring(PortalPathPrefix.Length);
                else
                {
                    // we enter an absolute webpath so we must reset portalPrefix to empty string
                    if ( nval.StartsWith("/") || nval.StartsWith("http:") || nval.StartsWith("https:") )
                    {
                        _portalPathPrefix   =   string.Empty;
                        PrefixAsPorTalPath  =   false;
                    }
                    innerValue              =   nval;
                }
				if ( SettingsTraceSwitch.Sw.TraceVerbose  || DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PortalUrlDataType Val SET _portalPathPrefix: {0}, innerValue: {1}", _portalPathPrefix,innerValue),(SettingsTraceSwitch.Sw.TraceVerbose) ? SettingsTraceSwitch.Sw.Info:DataTypeTraceSwitch.Sw.Info);

				//Reset _innerFullPath
				_innerFullPath              =   null;
			}
		}

		public override string Description
		{
			get
			{
                const string strDesc =   "Url relative to Portal";
				return strDesc;
			}
		}
	}
}